﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using GameUtils;
using ThouShaltNotBrick.Logic;
using ThouShaltNotBrick.Logic.GeneralObjects;
using ThouShaltNotBrick.Logic.Resources;
using ThouShaltNotBrick.Logic.Helpers;

namespace ThouShaltNotBrick.Logic.Helpers
{
    public static class DrawingHelpers
    {
        #region Methods
        /// <summary>
        /// Get pixels per meter in x-direction [pix/m].
        /// </summary>
        /// <param name="pScreenWidth">Screen width [pix].</param>
        /// <returns>Pixels per meter in x-direction [pix/m].</returns>
        public static float GetPixelsPerMeterX(float pScreenWidth)
        {
            return pScreenWidth * Constants.INVERSE_LEVEL_WIDTH;
        }

        /// <summary>
        /// Get pixels per meter in y-direction [pix/m].
        /// </summary>
        /// <param name="pScreenHeight">Screen height [pix].</param>
        /// <returns>Pixels per meter in y-direction [pix/m].</returns>
        public static float GetPixelsPerMeterY(float pScreenHeight)
        {
            return pScreenHeight * Constants.INVERSE_LEVEL_HEIGHT;
        }

        /// <summary>
        /// Invert y-offset [pix].
        /// </summary>
        /// <param name="pY">Non-inverted y-offset [pix].</param>
        /// <param name="pScreenHeight">Screen height [pix].</param>
        /// <returns>Inverted y-offset [pix].</returns>
        public static float GetInvertedY(float pY, float pScreenHeight)
        {
            return pScreenHeight - pY;
        }

        public static Rectangle GetPadDrawingRectangle(
            Pad pPad,
            float pScreenWidth,
            float pScreenHeight)
        {
            float pixelsPerMeterX = GetPixelsPerMeterX(pScreenWidth);
            float pixelsPerMeterY = GetPixelsPerMeterY(pScreenHeight);

            // position of bottom left corner [m]
            float padPositionX = pPad.Position - Constants.PAD_HALF_WIDTH;

            int rectX = (int)(padPositionX * pixelsPerMeterX);
            int rectY = (int)GetInvertedY((Constants.PAD_POSITION_Y + Constants.PAD_HEIGHT) * pixelsPerMeterY, pScreenHeight);
            int rectWidth = (int)(Constants.PAD_WIDTH * pixelsPerMeterX);
            int rectHeight = (int)(Constants.PAD_HEIGHT * pixelsPerMeterY);

            return new Rectangle(rectX, rectY, rectWidth, rectHeight);
        }

        /// <summary>
        /// Get ball's drawing rectangle.
        /// </summary>
        /// <param name="pBall">Ball.</param>
        /// <param name="pElapsed">Elapsed time since last update [s].</param>
        /// <param name="pScreenWidth">Screen width [pix].</param>
        /// <param name="pScreenHeight">Screen height [pix].</param>
        /// <returns>Balls drawing rectangle.</returns>
        public static Rectangle GetBallDrawingRectangle(
            Ball pBall,
            float pElapsed,
            float pScreenWidth,
            float pScreenHeight)
        {
            float pixelsPerMeterX = GetPixelsPerMeterX(pScreenWidth);
            float pixelsPerMeterY = GetPixelsPerMeterY(pScreenHeight);

            // position of center [m]
            float ballPositionX = pBall.Position.X;// + pElapsed * pBall.SpeedX;
            float ballPositionY = pBall.Position.Y;// + pElapsed * pBall.SpeedY;

            // position of center [pix]
            float ballXPix = ballPositionX * pixelsPerMeterX;
            float ballYPix = GetInvertedY(ballPositionY * pixelsPerMeterY, pScreenHeight);

            float ballRadiusXPix = pBall.Radius * pixelsPerMeterX;
            float ballRadiusYPix = pBall.Radius * pixelsPerMeterY;

            int rectX = (int)(ballXPix - ballRadiusXPix);
            int rectY = (int)(ballYPix - ballRadiusYPix);
            int rectWidth = (int)(2.0 * ballRadiusXPix);
            int rectHeight = (int)(2.0 * ballRadiusYPix);

            return new Rectangle(rectX, rectY, rectWidth, rectHeight);
        }

        public static Rectangle GetBrickDrawingRectangle(
            BrickWithCurrentState pBrick,
            float pScreenWidth,
            float pScreenHeight)
        {
            float pixelsPerMeterX = GetPixelsPerMeterX(pScreenWidth);
            float pixelsPerMeterY = GetPixelsPerMeterY(pScreenHeight);

            RectangleF brickRect = BrickHelpers.GetBrickRectangle(pBrick);

            int rectX = (int)(brickRect.X * pixelsPerMeterX);
            int rectY = (int)GetInvertedY(brickRect.Top * pixelsPerMeterY, pScreenHeight);
            int rectWidth = (int)(brickRect.Width * pixelsPerMeterX);
            int rectHeight = (int)(brickRect.Height * pixelsPerMeterY);

            return new Rectangle(rectX, rectY, rectWidth, rectHeight);
        }
        #endregion
    }
}